﻿CREATE PROCEDURE [acms].[FileDownload_Put]
	@Id int, 
	@FileId int,
	@UserId int,
	@IPAddress varchar(15),
	@Path varchar(2000),
	@Name varchar(100),
	@Created DateTime,
	@ConcurencyId rowversion,
	@IdOut int output,
	@ConcurencyIdOut rowversion output
AS
	if(@Id = 0) begin
		insert into FileDownload
			(FileId, UserId, IPAddress, Path, Name, Created)
		values
			(@FileId, @UserId, @IPAddress, @Path, @Name, @Created)
		set @IdOut = @@Identity
	end
	else if exists(select null from FileDownload_View where Id = @Id and ConcurencyId = @ConcurencyId) begin
		update FileDownload
		set
			FileId = @FileId,
			UserId = @UserId,
			IPAddress = @IPAddress,
			Path = @Path,
			Name = @Name
		where
			Id = @Id and ConcurencyId = @ConcurencyId
		set @IdOut = @Id
	end
	else if exists(select null from FileDownload_View where Id = @Id)
		return -100
	else 
		return -200
	
	select @ConcurencyIdOut = ConcurencyId From FileDownload_View where Id = @IdOut
	
RETURN 0;